package com.micro.commonlibrary.tools;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

//判断网
public class NetworkConnectionTools {
    private NetworkConnectionTools(){

    }
    public static boolean isWifiConnection() {
        ConnectivityManager cm = getConnectivityManager();
        NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null) {
            return networkInfo.isAvailable();
        }
        return false;
    }

    public static boolean isConnection() {
        ConnectivityManager cm = getConnectivityManager();
        NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
        return activeNetworkInfo.isConnected();
    }

    public static boolean isMobileConnection() {
        ConnectivityManager cm = getConnectivityManager();
        NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfo != null) {
            return networkInfo.isAvailable();
        }
        return false;
    }

    //   0 是 手机    1是WiFi  -1  没有获取到
    public static int getConnectionType() {
        ConnectivityManager connectivityManager = getConnectivityManager();
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            return activeNetworkInfo.getType();
        }
        return -1;
    }

    private static ConnectivityManager getConnectivityManager() {
        return (ConnectivityManager) UiTools.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    }
}
